#include "uv.h"

void UV::destroy()
{
	delete obj;
	obj = 0;
}

UV* UV::createObj()
{
	if (obj == 0)
		obj = new UV;
	return obj;
}

void UV::attach(Student_t* student)
{
	for (vector<Student_t*>::iterator iter = students.begin(); iter != students.end(); iter++)
	{
		if (*iter == student)
			return;
	}
	students.push_back(student);
}

void UV::detach(Student_t* student)
{
	for (vector<Student_t*>::iterator iter = students.begin(); iter != students.end(); iter++)
	{
		if (*iter == student)
			students.erase(iter);
	}
}

void UV::notify(int n)
{
	for (vector<Student_t*>::iterator iter = students.begin(); iter != students.end(); iter++)
	{
		(*iter)->update(this, n);
	}
}


void UV::addStudent(const unsigned int& id, const string& type)
{
	int error = 0;
	vector<Student_t*>::iterator iter;
	for (iter = students.begin(); iter != students.end(); iter++)
	{
		if((*iter)->getId() == id){
			error = 1;
			throw error;
		}
	}
	Student_t* student = new Student_t(id, type, this);
	attach(student);
}

void UV::removeStudent(const unsigned int& id)
{
	int error;
	vector<Student_t*>::iterator iter;
	for (iter = students.begin(); iter != students.end(); iter++)
	{
		if((*iter)->getId() == id)
			break;
	}
	if(iter == students.end()){
		error= 2;
		throw error;
	}
	detach(*iter);
}

void UV::raisePrice()
{
	notify(1);
}

void UV::cancelClass()
{
	notify(2);
}
